<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\model\Sort;
use app\common\model\Api;
use app\common\model\Question;
use app\common\model\User;
use think\Controller;
use think\Db;
use think\facade\Cache;
use think\Request;
use think\facade\Session;
use think\facade\Env;

class Main extends Controller
{
    //前置操作
    protected $beforeActionList = [
        'competence'
    ];
    //检测用户是否登陆
    protected function competence()
    {
        if (!Session::has('admin_user')) {
            return $this->redirect('admin/index/index');
        }
    }
    public function index()
    {
        return $this->fetch('admin/main/home');
    }
    //控制台首页
    public function console()
    {
        //展示数据
        // ~ 普通用户数量
        $info['user_num'] = User::where('grade', '0')->count();
        // ~ VIp用户数量
        $info['vip_num'] = User::where('grade', '1')->count();
        // ~ 推广员
        $info['gen_num'] = User::where('grade', '2')->count();
        // ~ 题库数量
        $info['question_num'] = Question::count();
        $version = '点击获取';
        $this->assign('version', $version);
        return $this->fetch('admin/main/index', ['info' => $info]);
    }
    // 退出登陆
    public function logout()
    {
        Session::clear();
        return $this->redirect('admin/index/index');
    }
    public function Xcache(){
        Cache::clear();
        return json(['code'=>1]);
    }
    //api 界面
    public function api()
    {
        $a = new Api();
        if ($this->request->isPost()) {
            $date = $this->request->param();
            if ($a->up($date)) {
                return json_x(0, '成功');
            } else {
                return json_x();
            }
        } else {
            $info = Api::get(1);
            return $this->fetch('admin/main/api', ['info' => $info]);
        }
    }

    //清理日志
    public function delRuntime()
    {
        $path = Env::get('runtime_path');
        $TEMP_PATH = $path . 'temp/';
        $LOG_PATH = $path . 'log/';
        if (delete_dir_file($TEMP_PATH) && delete_dir_file($LOG_PATH)) {
            $this->success('清除缓存成功!');
        } else {
            $this->error('清除缓存失败!');
        }
    }
}
